// pages/personal/personal.js
import http from "../../utils/baseApi.js";

Page({
	// 页面的初始数据
	data: {
		userInfo: null,
		strUserId: "",
    	nickname: "",
		avatar: "",
		mobile: "",
		arrSex: ["男","女"],
		sexIndex: 0,
		birthday: "1970-01-01",
		currentDay: ""
	},
	// 页面加载
	onLoad: function () {
		let that = this;
		let userInfo = wx.getStorageSync("userInfo");
		that.setData({ strUserId:userInfo.strUserId });
	},
	// 页面显示
	onShow: function () {
		let that = this;
		let userInfo = wx.getStorageSync("userInfo");
		let strUserAvatar = wx.getStorageSync("strUserAvatar");
		that.setData({ userInfo:userInfo, mobile:userInfo.strMobile, avatar:strUserAvatar });
		if (userInfo.strRealName != null) {
			that.setData({ nickname:userInfo.strRealName });
		}
		if (userInfo.strBirthday != "") {
			that.setData({ birthday:userInfo.strBirthday });
		}
		if (userInfo.intSex > 0) {
			that.setData({ sexIndex:(userInfo.intSex - 1) });
		}

		that.onGetCurrentDateTime();
		that.onGetUserBirthday();
		that.setData({ mobile:wx.getStorageSync("cellphone") });
	},
	// 通过资料获取用户资料中的年月日
	onGetUserBirthday: function () {
		let that = this;
		let userInfo = that.data.userInfo;
		let strUserBirthday = "1930-01-01";

		if (userInfo.strBirthday != null && userInfo.strBirthday != "") {
			strUserBirthday = userInfo.strBirthday;
		}

		that.setData({ birthday:strUserBirthday });
	},
	// 获取当前年月日
	onGetCurrentDateTime: function () {
		let that = this;
		let dateCurrentDate = new Date();
		let intFullYear = dateCurrentDate.getFullYear();
		let intMonth = dateCurrentDate.getMonth() + 1;
		let strMonth = (intMonth < 10) ? "0" + intMonth : intMonth;
		let strDate = (dateCurrentDate.getDate() < 10) ? "0" + dateCurrentDate.getDate() : dateCurrentDate.getDate();
		let strCurrentDateTime = intFullYear + "-" + strMonth + "-" + strDate;
		
		that.setData({ currentDay:strCurrentDateTime });
	},
	// 获取生日变更事件
	onChangeBirthday: function (e) {
		let that = this;
		that.setData({ birthday:e.detail.value });
	},
	// 更改性别事件
	onChangeSex: function (e) {
		let that = this;
		that.setData({ sexIndex:e.detail.value });
	},
	// 显示提示框toast方法抽取
	toast: function (msg) {
    	wx.showToast({ title:msg, icon:"none", duration:2000, mask:true });
	},
	// 更新用户昵称
	onInputRealname: function (e) {
		let that = this;
		that.setData({ nickname:e.detail.value });
	},
	// 保存个人资料
	save: function () {
		let that = this;
		let arrDate = that.data.birthday.split("-");
		let strUsername = that.data.nickname;
		let strMobile = that.data.mobile;
		let intSex = that.data.sexIndex;

		http.post("sale/information/edit",{
			strAvatar:wx.getStorageSync("strUserAvatar"),
			strUserId:that.data.userInfo.intUserId,
			strRealName:strUsername,
			strMobile:strMobile,
			sex:intSex,
			year:arrDate[0],
			month:arrDate[1],
			day:arrDate[2]
		}).then((res)=>{
			console.log(res);
			if (res.code == 2000) {
				wx.showToast({ title:"已保存!", icon:"success", mask:true, duration:1500 });
				let timer = setTimeout(()=>{
					clearTimeout(timer);
					wx.navigateBack(); 
				},1500);
			}
		});
	},
	// 更新用户手机号码
	onInputMobile: function (e) {
		let that = this;
		that.setData({ mobile:e.detail.value });
	},
	// 上传头像图片
	uploadAvatar: function (e) {
    	let that = this;
    	wx.chooseImage({
    		count: 1, // 默认9
    		sizeType: ["original","compressed"], // 可以指定是原图还是压缩图,默认二者都有
    		sourceType: ["album","camera"], // 可以指定来源是相册还是相机,默认二者都有
    		success: (res)=>{
        		// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        		let tempFilePaths = res.tempFilePaths
        		that.setData({avatar:tempFilePaths[0]});
        		wx.uploadFile({
        			url: "https://api.zetikeji.com/api/storage/file", //仅为示例，非真实的接口地址
        			filePath: tempFilePaths[0],
        			name: "file",
        			formData:{},
        			success: (res)=>{
            			if (res.statusCode == 200) {
            				let rets = res.data;
            				let data = JSON.parse(rets);
            				http.post("ucenter/save",{
                				userId: wx.getStorageSync("userId"),
                				avatar: data.data ? data.data : ""
            				}).then((ret) => {
                				if (ret.code == 2000) {                
                					that.toast("上传成功");
                				} else {
                					that.toast(ret.msg ? ret.msg : "小伙伴太热情了");
                				}
            				});
            			}
            		}
        		});
    		}
    	});
    }
})